import requests
import json
import datetime
import arrow

from pymongo import MongoClient


class DateTimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return arrow.Arrow.fromdatetime(obj).for_json()
        else:
            return json.JSONEncoder.default(self, obj)


target = ""

conn = MongoClient(target)
db = conn['wass']

post_url = "http://200-chengdu-yjc-scan2.intra.jiasule.com:910/v1/tasks:create"

query = {
    'task_config.is_disabled': False,
    'task_config.is_periodic': True,
    'addition.jobId': {'$in': ["623997edacdbd3001107feac", "6239980eacdbd3001107feb2", "62393f2dd53ea50011301ef1", "62399620a10051001130a44c", "623a88ceb0d6c80011fbd981"]}
    # 'addition.jobId': '62399620a10051001130a44c'
}


def main():
    for item in db['scan_tasks'].find(query):
        print(item.get('task_id'))
        # task_type = item.get('addition', {}).get('taskType')
        # next_start_at = item.get('next_start_at')
        #
        # data = {
        #     'task_id': item.get('task_id'),
        #     'target_url': item.get('target_url'),
        #     'origin_ip': item.get('origin_ip'),
        #     'job_names': item.get('job_names'),
        #     'filter_events': item.get('filter_events'),
        #     'task_config': item.get('task_config', {}),
        #     'job_config_map': item.get('job_config_map'),
        #     'flex_job_config_map': item.get('flex_job_config_map'),
        #     'target_id': item.get('target_id'),
        #     'user_id': item.get('user_id'),
        #     'addition': item.get('addition')
        # }
        # if next_start_at and task_type not in ['http', 'ping']:
        #     data['task_config']['start_at'] = item.get('next_start_at')
        #
        # resp = requests.post(url=post_url, data=json.dumps(data, cls=DateTimeEncoder))
        # if resp.status_code != 200:
        #     print(resp.text)
        #     print(item.get('task_id'))


if __name__ == '__main__':
    main()
